package com.yyh.BookLendSystem.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import java.sql.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.yyh.BookLendSystem.biz.BizException;
import com.yyh.BookLendSystem.biz.BookBiz;
import com.yyh.BookLendSystem.util.Utils;

import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class BookEditDlg extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text idText;
	private Text nameText;
	private Text authorText;
	private BookBiz biz =new BookBiz();
	private TableItem bookInfo;
	private DateTime pressdateDt;
	private Spinner priceSp;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public BookEditDlg(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	public BookEditDlg(Shell parent, int style, TableItem bookInfo) {
		this(parent, style);
		this.bookInfo=bookInfo;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(388, 339);
		shell.setText("图书编辑");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(59, 27, 61, 17);
		label.setText("编号");
		
		idText = 	new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		idText.setBounds(142, 24, 146, 23);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(59, 68, 61, 17);
		lblNewLabel.setText("书名");
		
		nameText = new Text(shell, SWT.BORDER);
		nameText.setBounds(142, 65, 146, 23);
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setText("作者");
		lblNewLabel_1.setBounds(59, 110, 61, 17);
		
		authorText = new Text(shell, SWT.BORDER);
		authorText.setBounds(142, 107, 146, 23);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(59, 158, 61, 17);
		label_1.setText("出版日期");
		
		pressdateDt = new DateTime(shell, SWT.BORDER);
		pressdateDt.setBounds(142, 151, 146, 24);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setBounds(59, 200, 61, 17);
		label_2.setText("价格");
		
		priceSp = new Spinner(shell, SWT.BORDER);
		priceSp.setBounds(142, 200, 146, 23);
		
		Button button = new Button(shell, SWT.NONE);
		
		button.setBounds(120, 254, 80, 27);
		button.setText("保存");
		
		Button button_1 = new Button(shell, SWT.NONE);
		
		button_1.setBounds(225, 254, 80, 27);
		button_1.setText("取消");
		Utils.centerWindows(shell);
		
		
		//////////////////////////
		//取消
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
				result = false;
			}
		});
		
		
		//保存
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				String id=idText.getText().trim();
				String name=nameText.getText().trim();
				String author = authorText.getText().trim();
				int y = pressdateDt.getYear();
				int m = pressdateDt.getMonth();
				int d = pressdateDt.getDay();
				String date = String.format("%d-%d-%d", y,m,d);
				Date pressdate =Date.valueOf(date);
				int price=Integer.valueOf(priceSp.getText());
				// biz+ dao
				try {
					biz.save(id, name, author, pressdate, price);
					Utils.showMessage(shell,  "保存成功");
					result = true;
					shell.close();
				} catch (BizException e1) {
					e1.printStackTrace();
					Utils.showMessage(shell, e1.getMessage() );
					result = false;
				}
				
			}
		});
		
		if(bookInfo!=null) {
			//获取表格中的字段
			
			idText.setText(bookInfo.getText(0));
			nameText.setText(bookInfo.getText(1));
			authorText.setText(bookInfo.getText(2));
			System.out.println(bookInfo.getText(4).isEmpty());
			if(bookInfo.getText(4).isEmpty()) {
				pressdateDt.setData(null);
			}else {
				int y = Integer.valueOf(bookInfo.getText(4).substring(0,4));
				int m = Integer.valueOf(bookInfo.getText(4).substring(5,7));
				int d = Integer.valueOf(bookInfo.getText(4).substring(8,10));
				pressdateDt.setDate(y, m-1, d);
			}	
			priceSp.setSelection(10);
		}
	
	}
}
